package com.zackratos.component1

import android.os.Bundle
import android.view.View
import com.zackratos.bottomrouter.BottomRouterManager
import com.zackratos.common.BaseComponentFragment

/**
 * @Author   : zhangwenchao
 * @Date     : 2020/12/24  9:16 PM
 * @Describe :
 */
class Component1Fragment: BaseComponentFragment("1") {

    companion object {
        fun newInstance() = Component1Fragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Component1Operator.getInstance().registerFragment(this)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnActionBottom.setOnClickListener {
            val content = "component1 bottom button click"
            with(BottomRouterManager.getInstance()) {
                component2?.updateBottomText(content)
                component3?.updateBottomText(content)
                component4?.updateBottomText(content)
            }
        }

        binding.btnActionTop.setOnClickListener {
            val content = "component1 top button click"
            TComponent1.getInstance().observable?.postClickTopButton(content)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Component1Operator.getInstance().unRegisterFragment()
    }

    fun updateBottomText(content: String?) {
        binding.tvContentBottom.text = content
    }

    fun updateTopText(content: String?) {
        binding.tvContentTop.text = content
    }
}